package com.ssu.judea.palmssu.util;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ssu.judea.palmssu.vo.GraphItem;
import com.ssu.judea.palmssu.vo.Member;
import com.ssu.judea.palmssu.vo.MemberItem;
import com.ssu.judea.palmssu.vo.Result;

import android.util.Log;


public class JSONParseHelper {
	public static Member parseToMember(JSONObject input){
		Member member = new Member();
		
		if(input!=null){
			try{
				if(input.getString("error").equals("")){
					member.setMemberseq(input.getInt("member_seq"));
					member.setProfileurl(input.getString("member_profile_url"));
					member.setTwitterid(input.getString("member_twitter_id"));
					return member;
				}else{
					return null;
				}
			}catch(JSONException e){
				Log.v("changdoc", e.getMessage());
				return null;
			}
		}else{
			return null;
		}		
	}
	
	public static Result parseToResult(JSONObject input){
		Result result = new Result();
		
		if(input!=null){
			try{
				result.setResult(input.getString("RESULT"));
				
				result.setMessage(input.getString("MESSAGE"));
				return result;
			}catch(JSONException e){
				Log.v("changdoc", e.getMessage());
				return null;
			}
		}else{
			return null;
		}	
	}

	public static ArrayList<MemberItem> parseToList(JSONArray jsonArray) {
		ArrayList<MemberItem> result = new ArrayList<MemberItem>();
		
		for(int i = 0; i<jsonArray.length();++i){
			
			MemberItem member = new MemberItem();
			try{
				member.setMemberseq(jsonArray.getJSONObject(i).getString("memberseq"));
				member.setImage(jsonArray.getJSONObject(i).getString("image"));
				member.setScreenname(jsonArray.getJSONObject(i).getString("screenname"));
				member.setStartdate(jsonArray.getJSONObject(i).getString("startdate"));
				member.setRoom(jsonArray.getJSONObject(i).getString("room"));
				member.setSeat(jsonArray.getJSONObject(i).getString("seat"));
				member.setComment(jsonArray.getJSONObject(i).getString("comment"));
				member.setMatchingrate(jsonArray.getJSONObject(i).getInt("matchingrate"));
			}catch(JSONException e){
				
			}
			result.add(member);
		}
		
		return result;
	}

	public static ArrayList<GraphItem> parseToGraphArrayList(String resultStr) {
		ArrayList<GraphItem> result = new ArrayList<GraphItem>();
		
		
		JSONArray jsonArr = null;
		try {
			jsonArr = new JSONArray(resultStr);
		} catch (JSONException e) {
			return null;
		}
		
		if(jsonArr!=null && jsonArr.length()>0){
			for(int i = 0; i<jsonArr.length();i++){
				JSONObject curJsonObj;
				try {
					curJsonObj = jsonArr.getJSONObject(i);
				} catch (JSONException e) {
					continue;
				}
				
				
				JSONArray starttimeJsonArr = null;
				try {
					starttimeJsonArr = curJsonObj.getJSONArray("starttime");
				 
					JSONArray endtimeJsonArr = curJsonObj.getJSONArray("endtime");
					
					float[] curstarttimeArray = new float[starttimeJsonArr.length()];
					float[] curendtimeArray = new float[endtimeJsonArr.length()];
					
					for(int j=0;j<starttimeJsonArr.length();j++){
						curstarttimeArray[j] = starttimeJsonArr.getInt(j);
					}
					for(int j=0;j<endtimeJsonArr.length();j++){
						curendtimeArray[j] = endtimeJsonArr.getInt(j);
					}
					GraphItem currentItem = new GraphItem(curJsonObj.getInt("memberseq"),
							null, curstarttimeArray, curendtimeArray);
					result.add(currentItem);
				}catch (JSONException e) {
					GraphItem currentItem = new GraphItem();
					try {
						currentItem.setMemberseq(curJsonObj.getInt("memberseq"));
					} catch (JSONException e1) {
						
					}
					result.add(currentItem);
				}
				
			}
		}
		
		
		return result;
	}

	public static ArrayList<Member> parseToFollowersInfo(String string) {
		
		ArrayList<Member> resultList = new ArrayList<Member>();
		Member mine = new Member();
		mine.setTwitterid("Me");
		mine.setMemberseq(0);
		resultList.add(mine);
		
		try {
			JSONArray jsonArr = new JSONArray(string);
			
			if(jsonArr!=null && jsonArr.length()>0){
				for(int i = 0; i<jsonArr.length();i++){
					JSONObject curJson = jsonArr.getJSONObject(i);
					
					Member cur = new Member();
					cur.setMemberseq(curJson.getInt("memberseq"));
					cur.setTwitterid(curJson.getString("twitterid"));
					
					resultList.add(cur);
				}
			}
			
		} catch (JSONException e) {
			return resultList;
		}
		
		return resultList;
	}
}
